package com.shui_shui.mainload;

import java.io.IOException;

import com.shui_shui.R;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.MeasureSpec;

public class GIFView extends View{        
    private Movie movie;    
    private long moviestart;  
    public static String gif;
    public GIFView(Context context) throws IOException {  
        super(context);
    movie=Movie.decodeStream(getResources().getAssets().open(gif));
    }
    public GIFView(Context context, AttributeSet attrs) throws IOException{
        super(context, attrs);
    movie=Movie.decodeStream(getResources().getAssets().open(gif));
    }
    public GIFView(Context context, AttributeSet attrs, int defStyle) throws IOException {
        super(context, attrs, defStyle);
        movie=Movie.decodeStream(getResources().getAssets().open(gif));
    }
    @SuppressLint("NewApi")
	@Override  
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        long now=android.os.SystemClock.uptimeMillis();
        int px = getMeasuredWidth()/2;
		int py = getMeasuredHeight()/2;
        if (moviestart == 0) 
            moviestart = now;
            int relTime;
            relTime = (int)((now - moviestart) % movie.duration());
            movie.setTime(relTime);
            movie.draw(canvas,px-(movie.width()/2),py-(movie.height()/2));
            this.invalidate();
    }
    @Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
		
		int measureWidth = measure(widthMeasureSpec);
		int measureHeight = measure(heightMeasureSpec);
		setMeasuredDimension(measureWidth,measureHeight);
		
	}
	
	private int measure(int measureSpec){
		int result = 0;
		// Decode the measurement specifications.
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		if (specMode == MeasureSpec.UNSPECIFIED) {
			// Return a default size of 200 if no bounds are specified.
			result = 200;
		} else {
			// As you want to fill the available space
			// always return the full available bounds.
			result = specSize;
		}
		return result;
		
	}
}    